from  sklearn.datasets import load_wine    # 数据集
from  sklearn.decomposition import PCA    # PCA降维
from sklearn.model_selection import train_test_split    # 数据集划分：HoldOut法
from sklearn.preprocessing import StandardScaler    # 标准化
from sklearn.model_selection import GridSearchCV    # 网格搜索
from sklearn.neighbors import KNeighborsClassifier    # KNN分类器
import numpy as np


def knn_wine():
    """
    用KNN算法对葡萄酒数据集进行分类
    :return:
    """
    # 1.加载数据集
    wine = load_wine()
    # print(wine.data.shape)    # (178, 13)
    # print(wine.target_names)  # 3种

    # 2.划分数据集 -> HoldOut法
    x_train, x_test, y_train, y_test = train_test_split(wine.data, wine.target,
                                                        test_size=0.2,
                                                        random_state=22,
                                                        stratify=wine.target)
    # 3.特征工程：标准化
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4.PCA降维
    pca =  PCA(n_components=0.8)
    x_train = pca.fit_transform(x_train)
    x_test = pca.transform(x_test)

    # 5.KNN模型训练 + 网格搜素
    # 创建KNN模型
    estimator = KNeighborsClassifier()
    # 定义网格搜索参数
    param_grid = {"n_neighbors": [1, 3, 5, 7, 9, 11]}
    # 进行网格搜索：会自动交叉验证
    model = GridSearchCV(estimator, param_grid=param_grid, cv=5)    # cv=5表示交叉验证5折
    # 训练模型
    model.fit(x_train, y_train)
    # 查看训练结果
    print("最佳参数:", model.best_params_)
    print("最佳分数:", model.best_score_)

    # 6. 模型评估
    # 预测，并对比真实值
    y_predict = model.predict(x_test)
    true_array = np.array(y_test == y_predict)
    print(f"正确个数：{np.sum(true_array)} / {len(true_array)}")
    # 直接查看准确率
    print("准确率：", model.score(x_test, y_test))



if __name__ == '__main__':
    knn_wine()